<?php
echo nl2br ( whois_query ( 'invisible.ir' ) );
function whois_query($domain) {
	
	// fix the domain name:
	$domain = strtolower ( trim ( $domain ) );
	$domain = preg_replace ( '/^http:\/\//i', '', $domain );
	$domain = preg_replace ( '/^www\./i', '', $domain );
	$domain = explode ( '/', $domain );
	$domain = trim ( $domain [0] );
	
	// split the TLD from domain name
	$_domain = explode ( '.', $domain );
	$lst = count ( $_domain ) - 1;
	$ext = $_domain [$lst];
	
	// You find resources and lists
	// like these on wikipedia:
	//
	// http://de.wikipedia.org/wiki/Whois
	//
	/* $servers = array (
			"biz" => "whois.neulevel.biz",
			"com" => "whois.internic.net",
			"us" => "whois.nic.us",
			"coop" => "whois.nic.coop",
			"info" => "whois.nic.info",
			"name" => "whois.nic.name",
			"net" => "whois.internic.net",
			"gov" => "whois.nic.gov",
			"edu" => "whois.internic.net",
			"mil" => "rs.internic.net",
			"int" => "whois.iana.org",
			"ac" => "whois.nic.ac",
			"ae" => "whois.uaenic.ae",
			"at" => "whois.ripe.net",
			"au" => "whois.aunic.net",
			"be" => "whois.dns.be",
			"bg" => "whois.ripe.net",
			"br" => "whois.registro.br",
			"bz" => "whois.belizenic.bz",
			"ca" => "whois.cira.ca",
			"cc" => "whois.nic.cc",
			"ch" => "whois.nic.ch",
			"cl" => "whois.nic.cl",
			"cn" => "whois.cnnic.net.cn",
			"cz" => "whois.nic.cz",
			"de" => "whois.nic.de",
			"fr" => "whois.nic.fr",
			"hu" => "whois.nic.hu",
			"ie" => "whois.domainregistry.ie",
			"il" => "whois.isoc.org.il",
			"in" => "whois.ncst.ernet.in",
			"ir" => "whois.nic.ir",
			"mc" => "whois.ripe.net",
			"to" => "whois.tonic.to",
			"tv" => "whois.tv",
			"ru" => "whois.ripn.net",
			"org" => "whois.pir.org",
			"aero" => "whois.information.aero",
			"nl" => "whois.domain-registry.nl" 
	); */
	
	$servers = array(
			"ac"    => "whois.nic.ac",
			"ae"    => "whois.nic.ae",
			"aero"  => "whois.aero",
			"af"    => "whois.nic.af",
			"ag"    => "whois.nic.ag",
			"al"    => "whois.ripe.net",
			"am"    => "whois.amnic.net",
			"arpa"  => "whois.iana.org",
			"as"    => "whois.nic.as",
			"asia"  => "whois.nic.asia",
			"at"    => "whois.nic.at",
			"au"    => "whois.aunic.net",
			"az"    => "whois.ripe.net",
			"ba"    => "whois.ripe.net",
			"be"    => "whois.dns.be",
			"bg"    => "whois.register.bg",
			"bi"    => "whois.nic.bi",
			"biz"   => "whois.biz",
			"bj"    => "whois.nic.bj",
			"br"    => "whois.registro.br",
			"bt"    => "whois.netnames.net",
			"by"    => "whois.ripe.net",
			"bz"    => "whois.belizenic.bz",
			"ca"    => "whois.cira.ca",
			"cat"   => "whois.cat",
			"cc"    => "whois.nic.cc",
			"cd"    => "whois.nic.cd",
			"ch"    => "whois.nic.ch",
			"ci"    => "whois.nic.ci",
			"ck"    => "whois.nic.ck",
			"cl"    => "whois.nic.cl",
			"cn"    => "whois.cnnic.net.cn",
			"com"   => "whois.verisign-grs.com",
			"coop"  => "whois.nic.coop",
			"cx"    => "whois.nic.cx",
			"cy"    => "whois.ripe.net",
			"cz"    => "whois.nic.cz",
			"de"    => "whois.denic.de",
			"dk"    => "whois.dk-hostmaster.dk",
			"dm"    => "whois.nic.cx",
			"dz"    => "whois.ripe.net",
			"edu"   => "whois.educause.edu",
			"ee"    => "whois.eenet.ee",
			"eg"    => "whois.ripe.net",
			"es"    => "whois.ripe.net",
			"eu"    => "whois.eu",
			"fi"    => "whois.ficora.fi",
			"fo"    => "whois.ripe.net",
			"fr"    => "whois.nic.fr",
			"gb"    => "whois.ripe.net",
			"gd"    => "whois.adamsnames.com",
			"ge"    => "whois.ripe.net",
			"gg"    => "whois.channelisles.net",
			"gi"    => "whois2.afilias-grs.net",
			"gl"    => "whois.ripe.net",
			"gm"    => "whois.ripe.net",
			"gov"   => "whois.nic.gov",
			"gr"    => "whois.ripe.net",
			"gs"    => "whois.nic.gs",
			"gw"    => "whois.nic.gw",
			"gy"    => "whois.registry.gy",
			"hk"    => "whois.hkirc.hk",
			"hm"    => "whois.registry.hm",
			"hn"    => "whois2.afilias-grs.net",
			"hr"    => "whois.ripe.net",
			"hu"    => "whois.nic.hu",
			"ie"    => "whois.domainregistry.ie",
			"il"    => "whois.isoc.org.il",
			"in"    => "whois.inregistry.net",
			"info"  => "whois.afilias.info",
			"int"   => "whois.iana.org",
			"io"    => "whois.nic.io",
			"iq"    => "vrx.net",
			"ir"    => "whois.nic.ir",
			"is"    => "whois.isnic.is",
			"it"    => "whois.nic.it",
			"je"    => "whois.channelisles.net",
			"jobs"  => "jobswhois.verisign-grs.com",
			"jp"    => "whois.jprs.jp",
			"ke"    => "whois.kenic.or.ke",
			"kg"    => "www.domain.kg",
			"ki"    => "whois.nic.ki",
			"kr"    => "whois.nic.or.kr",
			"kz"    => "whois.nic.kz",
			"la"    => "whois.nic.la",
			"li"    => "whois.nic.li",
			"lt"    => "whois.domreg.lt",
			"lu"    => "whois.dns.lu",
			"lv"    => "whois.nic.lv",
			"ly"    => "whois.nic.ly",
			"ma"    => "whois.iam.net.ma",
			"mc"    => "whois.ripe.net",
			"md"    => "whois.ripe.net",
			"me"    => "whois.meregistry.net",
			"mg"    => "whois.nic.mg",
			"mil"   => "whois.nic.mil",
			"mn"    => "whois.nic.mn",
			"mobi"  => "whois.dotmobiregistry.net",
			"ms"    => "whois.adamsnames.tc",
			"mt"    => "whois.ripe.net",
			"mu"    => "whois.nic.mu",
			"museum"=> "whois.museum",
			"mx"    => "whois.nic.mx",
			"my"    => "whois.mynic.net.my",
			"na"    => "whois.na-nic.com.na",
			"name"  => "whois.nic.name",
			"net"   => "whois.verisign-grs.net",
			"nf"    => "whois.nic.nf",
			"nl"    => "whois.domain-registry.nl",
			"no"    => "whois.norid.no",
			"nu"    => "whois.nic.nu",
			"nz"    => "whois.srs.net.nz",
			"org"   => "whois.pir.org",
			"pl"    => "whois.dns.pl",
			"pm"    => "whois.nic.pm",
			"pr"    => "whois.uprr.pr",
			"pro"   => "whois.registrypro.pro",
			"pt"    => "whois.dns.pt",
			"re"    => "whois.nic.re",
			"ro"    => "whois.rotld.ro",
			"ru"    => "whois.ripn.net",
			"sa"    => "whois.nic.net.sa",
			"sb"    => "whois.nic.net.sb",
			"sc"    => "whois2.afilias-grs.net",
			"se"    => "whois.iis.se",
			"sg"    => "whois.nic.net.sg",
			"sh"    => "whois.nic.sh",
			"si"    => "whois.arnes.si",
			"sk"    => "whois.ripe.net",
			"sm"    => "whois.ripe.net",
			"st"    => "whois.nic.st",
			"su"    => "whois.ripn.net",
			"tc"    => "whois.adamsnames.tc",
			"tel"   => "whois.nic.tel",
			"tf"    => "whois.nic.tf",
			"th"    => "whois.thnic.net",
			"tj"    => "whois.nic.tj",
			"tk"    => "whois.dot.tk",
			"tl"    => "whois.nic.tl",
			"tm"    => "whois.nic.tm",
			"tn"    => "whois.ripe.net",
			"to"    => "whois.tonic.to",
			"tp"    => "whois.nic.tl",
			"tr"    => "whois.nic.tr",
			"travel"=> "whois.nic.travel",
			"tv"    => "tvwhois.verisign-grs.com",
			"tw"    => "whois.twnic.net.tw",
			"ua"    => "whois.net.ua",
			"ug"    => "whois.co.ug",
			"uk"    => "whois.nic.uk",
			"us"    => "whois.nic.us",
			"uy"    => "nic.uy",
			"uz"    => "whois.cctld.uz",
			"va"    => "whois.ripe.net",
			"vc"    => "whois2.afilias-grs.net",
			"ve"    => "whois.nic.ve",
			"vg"    => "whois.adamsnames.tc",
			"wf"    => "whois.nic.wf",
			"ws"    => "whois.website.ws",
			"yt"    => "whois.nic.yt",
			"yu"    => "whois.ripe.net"
	);
	if (! isset ( $servers [$ext] )) {
		die ( 'Error: No matching nic server found!' );
	}
	
	$nic_server = $servers [$ext];
	
	$output = '';
	
	// connect to whois server:
	if ($conn = fsockopen ( $nic_server, 43 )) {
		fputs ( $conn, $domain . "\r\n" );
		while ( ! feof ( $conn ) ) {
			$output .= fgets ( $conn, 128 );
		}
		fclose ( $conn );
	} else {
		die ( 'Error: Could not connect to ' . $nic_server . '!' );
	}
	
	return $output;
}

function getwhois($domain, $tld)
{
	$whois = new Whois();

	if( !$whois->ValidDomain($domain.'.'.$tld) ){
		return 'Sorry, the domain is not valid or not supported.';
	}

	if( $whois->Lookup($domain.'.'.$tld) )
	{
		return $whois->GetData(1);
	}else{
		return 'Sorry, an error occurred.';
	}
}

class Whois {
	var $m_status = 0;
	var $m_domain = '';
	var $m_servers = array ();
	var $m_data = array ();
	var $m_connectiontimeout = 5;
	var $m_sockettimeout = 30;
	var $m_redirectauth = true;
	var $m_usetlds = array ();
	var $m_supportedtlds = array ();
	var $m_serversettings = array ();
	function Whois() {
		$this->readconfig ();
	}
	function readconfig() {
		$this->m_serversettings = array ();
		$this->m_tlds = array ();
		$this->m_usetlds = array ();
		
		$servers = array (
				"whois.crsnic.net#domain |No match for |Whois Server:|>NOTICE: The expiration date |Registrar:#Status:#Expiration Date:",
				"whois.afilias.net|NOT FOUND||<you agree to abide by this policy.|Expiration Date:#Status:#Registrant Email:#Admin Name:#Billing Name:#Billing Email#Tech Name:#Tech Email:#Registrant Name:#Admin Email:#Name Server:",
				"whois.nic.us|Not found:||>NeuStar, Inc., the Registry Administrator|Domain Expiration Date:#Domain Status:#Sponsoring Registrar:#Registrant Name:#Registrant Email:#Administrative Contact Name:#Administrative Contact Email:#Billing Contact Name:#Billing Contact Email:#Technical Contact Name:#Technical Contact Email:#Name Server:",
				"whois.internic.net|No match for |Whois Server:",
				"whois.publicinterestregistry.net|NOT FOUND||<you agree to abide by this policy.|Expiration Date:#Status:#Name Server:#Registrant Name:#Registrant Email:#Admin Name:#Admin Email:#Tech Name:#Tech Email:#Billing Name:#Billing Email:",
				"whois.neulevel.biz|Not found:||>NeuLevel, Inc., the Registry|Domain Expiration Date:#Domain Status:#Sponsoring Registrar:#Registrant Name:#Registrant Email:#Administrative Contact Name:#Administrative Contact Email:#Billing Contact Name:#Billing Contact Email:#Technical Contact Name:#Technical Contact Email:#Name Server:",
				"whois.nic.uk|No match for|||Registration Status:#Registrant:#Registrant's Address:#Renewal Date:#Name servers",
				"rs.domainbank.net|||<of the foregoing policies.|Administrative Contact:#Record expires on #Technical Contact:#Registrant:#Zone Contact:#Domain servers in ",
				"whois.moniker.com|||<you agree to abide by this policy.|Administrative Contact:#Registrant:#Domain Servers#Billing Contact:#Technical Contact:#Domain Expires on",
				"whois.networksolutions.com|||<right to modify these terms at any time.|Registrant:#Administrative Contact:#Record expires on #Domain servers in listed order:",
				"whois.enom.com|||>The data in this whois database |Registrant Contact:#Technical Contact:#Billing Contact:#Administrative Contact:#Status:#Name Servers:#Expiration date:",
				"whois.opensrs.net|||>The Data in the Tucows Registrar|Registrant:#Administrative Contact:#Technical Contact:#Record expires on#Domain servers in listed order:",
				"whois.godaddy.com|||<domain names listed in this database.|Registrant:#Expires On:#Administrative Contact:#Technical Contact:#Domain servers in listed order:",
				"whois.aunic.net|No Data Found|||Status:#Registrant Contact Name:#Registrant Email:#Name Server:#Tech Name:#Tech Email:",
				"whois.denic.de|free",
				"whois.worldsite.ws|No match for|||Registrant:#Name Servers:",
				"whois.nic.tv|",
				"whois.nic.tm|No match for",
				"whois.cira.ca|AVAIL",
				"whois.nic.cc|No match|Whois Server:|>The Data in eNIC Corporation|Whois Server:#Updated:",
				"whois.domainzoo.com|||<you agree to abide by these terms.",
				"whois.domaindiscover.com|||<you agree to abide by this policy.",
				"whois.markmonitor.com|||<you agree to abide by this policy.",
				"whois2.afilias-grs.net|NOT FOUND||<abide by this policy." 
		);
		$tlds = array (
				"com=whois.crsnic.net",
				"net=whois.crsnic.net",
				"org=whois.publicinterestregistry.net",
				"info=whois.afilias.net",
				"biz=whois.neulevel.biz",
				"us=whois.nic.us",
				"co.uk=whois.nic.uk",
				"org.uk=whois.nic.uk",
				"ltd.uk=whois.nic.uk",
				"ca=whois.cira.ca",
				"cc=whois.nic.cc",
				"edu=whois.crsnic.net",
				"com.au=whois.aunic.net",
				"net.au=whois.aunic.net",
				"de=whois.denic.de",
				"ws=whois.worldsite.ws",
				"sc=whois2.afilias-grs.net" 
		);
		
		$cnt = count ( $servers );
		
		foreach ( $servers as $server ) {
			$server = trim ( $server );
			$bits = explode ( '|', $server );
			if (count ( $bits ) > 1) {
				for($i = count ( $bits ); $i < 5; $i ++) {
					if (! isset ( $bits [$i] ))
						$bits [$i] = '';
				}
				$server = explode ( "#", $bits [0] );
				if (! isset ( $server [1] ))
					$server [1] = '';
				
				$this->m_serversettings [$server [0]] = array (
						'server' => $server [0],
						'available' => $bits [1],
						'auth' => $bits [2],
						'clean' => $bits [3],
						'hilite' => $bits [4],
						'extra' => $server [1] 
				);
			}
		}
		foreach ( $tlds as $tld ) {
			$tld = trim ( $tld );
			$bits = explode ( '=', $tld );
			if (count ( $bits ) == 2 && $bits [0] != '' && isset ( $this->m_serversettings [$bits [1]] )) {
				$this->m_usetlds [$bits [0]] = true;
				$this->m_tlds [$bits [0]] = $bits [1];
			}
		}
	}
	function SetTlds($tlds = 'com,net,org,info,biz,us,co.uk,org.uk') {
		$tlds = strtolower ( $tlds );
		$tlds = explode ( ',', $tlds );
		$this->m_usetlds = array ();
		foreach ( $tlds as $t ) {
			$t = trim ( $t );
			if (isset ( $this->m_tlds [$t] ))
				$this->m_usetlds [$t] = true;
		}
		return count ( $this->m_usetlds );
	}
	function Lookup($domain) {
		$domain = strtolower ( $domain );
		$this->m_servers = array ();
		$this->m_data = array ();
		$this->m_tld = $this->m_sld = '';
		$this->m_domain = $domain;
		
		if ($this->splitdomain ( $this->m_domain, $this->m_sld, $this->m_tld )) {
			$this->m_servers [0] = $this->m_tlds [$this->m_tld];
			$this->m_data [0] = $this->dolookup ( $this->m_serversettings [$this->m_servers [0]] ['extra'] . $domain, $this->m_servers [0] );
			if ($this->m_data [0] != '') {
				if ($this->m_serversettings [$this->m_servers [0]] ['auth'] != '' && $this->m_redirectauth && $this->m_status == 'STATUS_UNAVAILABLE') {
					if (preg_match ( '/' . $this->m_serversettings [$this->m_servers [0]] ['auth'] . '(.*)/i', $this->m_data [0], $match )) {
						$server = trim ( $match [1] );
						if ($server != '') {
							$this->m_servers [1] = $server;
							$command = isset ( $this->m_serversettings [$this->m_servers [1]] ['extra'] ) ? $this->m_serversettings [$this->m_servers [1]] ['extra'] : '';
							$dt = $this->dolookup ( $command . $this->m_domain, $this->m_servers [1] );
							$this->m_data [1] = $dt;
						}
					}
				}
				
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
	function ValidDomain($domain) {
		$domain = strtolower ( $domain );
		return $this->splitdomain ( $domain, $sld, $tld );
	}
	function GetDomain() {
		return $this->m_domain;
	}
	function GetServer($i = 0) {
		return isset ( $this->m_servers [$i] ) ? $this->m_servers [$i] : '';
	}
	function GetData($i = -1) {
		if ($i != - 1 && isset ( $this->m_data [$i] )) {
			$dt = htmlspecialchars ( trim ( $this->m_data [$i] ) );
			$this->cleandata ( $this->m_servers [$i], $dt );
			return $dt;
		} else {
			return trim ( join ( "\n", $this->m_data ) );
		}
		return '';
	}
	function splitdomain($domain, &$sld, &$tld) {
		$domain = strtolower ( $domain );
		$sld = $tld = '';
		$domain = trim ( $domain );
		$pos = strpos ( $domain, '.' );
		if ($pos != - 1) {
			$sld = substr ( $domain, 0, $pos );
			$tld = substr ( $domain, $pos + 1 );
			if (isset ( $this->m_usetlds [$tld] ) && $sld != '')
				return true;
		} else {
			$tld = $domain;
		}
		return false;
	}
	function whatserver($domain) {
		$sld = $tld = '';
		$this->splitdomain ( $domain, $sld, $tld );
		$server = isset ( $this->m_usetlds [$tld] ) ? $this->m_tlds [$tld] : '';
		return $server;
	}
	function dolookup($domain, $server) {
		$domain = strtolower ( $domain );
		$server = strtolower ( $server );
		if ($domain == '' || $server == '')
			return false;
		
		$data = "";
		$fp = @fsockopen ( $server, 43, $errno, $errstr, $this->m_connectiontimeout );
		if ($fp) {
			@fputs ( $fp, $domain . "\r\n" );
			@socket_set_timeout ( $fp, $this->m_sockettimeout );
			while ( ! @feof ( $fp ) ) {
				$data .= @fread ( $fp, 4096 );
			}
			@fclose ( $fp );
			
			return $data;
		} else {
			return "\nError - could not open a connection to $server\n\n";
		}
	}
	function cleandata($server, &$data) {
		if (isset ( $this->m_serversettings [$server] )) {
			$clean = $this->m_serversettings [$server] ['clean'];
			if ($clean != '') {
				$from = $clean [0];
				if ($from == '>' || $from == '<') {
					$clean = substr ( $clean, 1 );
					$pos = strpos ( strtolower ( $data ), strtolower ( $clean ) );
					if ($pos !== false) {
						if ($from == '>') {
							$data = trim ( substr ( $data, 0, $pos ) );
						} else {
							$data = trim ( substr ( $data, $pos + strlen ( $clean ) ) );
						}
					}
				}
			}
		}
	}
}
	
